package com.project.shortlink.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.project.shortlink.util.ShortLinkThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("新增数据自动填充开始...");

        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createUser", Long.class, ShortLinkThreadLocal.get());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateUser",Long.class, ShortLinkThreadLocal.get());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("更新数据自动填充开始...");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateUser", Long.class, ShortLinkThreadLocal.get());
    }
}
